class: center, middle, inverse, title-slide # Interactive graphics ## SISBID 2017
https://github.com/SISBID/Module2
### Di Cook (
dicook@monash.edu
,
@visnut
)
Heike Hofmann (
heike.hofmann@gmail.com
,
@heike_hh
) ### 07/12-14/2017 --- # Choices of packages - `ggvis`: both static and interactive graphics, interactive is very much a work in progress (Wickham) - `plotly`: has come a long way in the last 12 months, part of Carson Sievert's PhD thesis research. The beauty is that is builds directly onto ggplot2 - `animint`: Hasn't progressed much in the last year, needs special copy of ggplot2 (Hocking, et al) - `htmlwidgets`: has progressed a lot in 12 months, providing the base for other packages, e.g. `plotly` (Chang) - `rCharts`, `rbokeh`, `gridSVG`, `epivizr`, `cranvas` --- # ggvis Built on the javascript library `vega` ```r library(ggvis) data("economics", package = "ggplot2") ggvis(economics, x=~date, y=~psavert) ```
Renderer:
SVG
|
Canvas
Download
--- # ```r economics %>% ggvis(~date, ~psavert) %>% layer_points() %>% layer_smooths() ```
Renderer:
SVG
|
Canvas
Download
--- # Interactivity The coolest thing about ggvis is that plot parameters don't need to be static: they can be interactive - `input_text()`: text box - `input_slider()`: numeric slider - `input_select()`: dropdown box - `input_checkbox()`: checkbox --- # ```r economics %>% ggvis(~date, ~psavert) %>% layer_points(opacity := 0.2) %>% layer_smooths( span = input_slider(0.02, 0.5), stroke := "red" ) ```
Renderer:
SVG
|
Canvas
Download
--- # Attention grabber - ggvis grabs the R console - you cannot do any other R commands while an interactive ggvis plot is showing - exit by clicking on the `stop` icon on the plot panel ---  --- class: inverse middle # Your turn  The values provided for the slider (0.2, 1) look like they are not ideal. Change slider bounds so that they are more appropriate for this data. --- # Labels ```r all_values <- function(x) { if (is.null(x)) return(NULL) paste0(names(x), ": ", format(x), collapse = "<br />") } economics %>% ggvis(~unemploy, ~psavert) %>% layer_points() %>% add_tooltip(all_values, "hover") ``` ---
Renderer:
SVG
|
Canvas
Download
--- # Checkbox ```r model_type <- input_checkbox(label = "Use loess curve", map = function(val) if(val) "loess" else "lm") economics %>% ggvis(~date, ~psavert) %>% layer_points() %>% layer_model_predictions( model = model_type, span = input_slider(0.02, 0.5)) ``` --- # --- class: inverse middle # Your turn  - Convert the model input to two radio buttons - Add a robust linear model choice, which means adding a third radio button --- # plotly The `plotly` package in R builds on the `ggplot2` package, adding interactive elements to these plots. It translates plots to javascript. ```r library(plotly) #plot_ly(economics, x = date, y = unemploy / pop) ``` --- --- # Or using ggplot2 ```r ggplot(data=economics, aes(x = date, y = unemploy / pop)) + geom_point() + geom_line() ``` <img src="index_files/figure-html/unnamed-chunk-13-1.png" style="display: block; margin: auto;" /> --- ```r ggplotly() ```
--- --- # Still a work in progress ```r library(GGally) p <- ggpairs(economics[,3:6]) ggplotly(p) ```
--- # ```r data(canada.cities, package = "maps") viz <- ggplot(canada.cities, aes(long, lat)) + borders(regions = "canada") + coord_equal() + geom_point(aes(text = name, size = pop), colour = "red", alpha = 1/2) ggplotly(viz) ```
--- --- # Return to the RNA-Seq data <img src="index_files/figure-html/unnamed-chunk-17-1.png" style="display: block; margin: auto;" /> --- ```r p <- ggplot(sig.tab, aes(x=C_S1_R2, y=C_S2_R1, label=genes)) + geom_point(alpha=0.1) ggplotly(p) ```
With a large data set it can be slow! --- class: inverse middle # Your turn  Find which `ggplot2` geoms are supported in plotly, and maybe one that is not? --- # Example, using Australian elections ```r library(eechidna) launchApp( age = c("Age20_24", "Age85plus"), religion = c("Christianity", "Catholic", "NoReligion"), other = c("Unemployed", "Population", "MedianIncome") ) ``` --- class: inverse middle # Your turn  - Go to the `plotly` [github page examples](https://github.com/ropensci/plotly/tree/master/inst/examples) and play with some of the examples. - Take one of the ggplot2 plots we have made so far in the workshop, and add interactive labelling to it. --- # Resources - [ggvis](http://ggvis.rstudio.com) - [plotly](https://plot.ly/r/getting-started/) --- # Share and share alike This work is licensed under the Creative Commons Attribution-Noncommercial 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.